import subprocess
import logging
import argparse

# Configure logging
logging.basicConfig(filename='switch_partition.log', level=logging.INFO, 
                    format='%(asctime)s - %(levelname)s - %(message)s')

def log_message(message):
    """Log a message to both console and log file."""
    print(message)
    logging.info(message)

def run_command(command):
    """Run a shell command and handle errors."""
    try:
        result = subprocess.run(command, check=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output = result.stdout.decode('utf-8')
        log_message(output)
    except subprocess.CalledProcessError as e:
        error_message = f"Error executing command: {e.cmd}\n{e.stderr.decode('utf-8')}"
        log_message(error_message)
        exit(1)

def switch_partition(partition):
    """Switch to a different partition."""
    log_message(f"Switching to partition: {partition}")
    run_command(f"sudo ostree admin deploy {partition}")

def main():
    parser = argparse.ArgumentParser(description='Switch to a different OSTree partition.')
    parser.add_argument('partition', type=str, help='The name of the partition to switch to.')
    args = parser.parse_args()
    
    # Perform partition switch
    switch_partition(args.partition)

if __name__ == "__main__":
    main()

